﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.Objects.DataClasses;

using FinalProject.BL;
using FinalProject.DAL.Model;

namespace FinalProject.UI
{
    /// <summary>
    /// Interaction logic for LookupForm.xaml
    /// </summary>
    public partial class LookupForm : Window
    {
        #region Custom events
        public delegate void FormSendSectedHandler(object sender);
        public event FormSendSectedHandler FormSendSelected;
        private void OnFormSendSelected(object sender, EventArgs e)
        {
            if (FormSendSelected != null)
            {
                FormSendSelected(sender);
            }
        }
        #endregion

        protected Type _persistenceType;

        public Guid SelectedId
        {
            get
            {
                Guid result = Guid.Empty;
                if (!this.grvData.SelectedCells.IsNullOrEmpty() &&
                    this.grvData.SelectedCells[0] != null)
                {
                    result = this.grvData.SelectedCells[0].ToGuid();
                }
                return result;
            }
        }

        public LookupForm()
        {
            this.InitializeComponent();
            this.grvData.AutoGenerateColumns = false;
            this.grvData.IsReadOnly = true;

            this.InitialColumnForGrid();
            this.IntializedEvents();
            // Insert code required on object creation below this point.
        }

        public LookupForm(Type persistenceType)
            : this()
        {
            this._persistenceType = persistenceType;
            this.InitializeData();
        }

        private void InitialColumnForGrid()
        {
            DataGridTextColumn colId, colCode, colName;

            colId = new DataGridTextColumn();
            colId.Binding = new Binding("Id");
            colId.Visibility = System.Windows.Visibility.Hidden;
            this.grvData.Columns.Add(colId);

            colCode = new DataGridTextColumn()
            {
                Binding = new Binding("Number"),
                Header = "Number",
                Width = 150

            };
            this.grvData.Columns.Add(colCode);

            colName = new DataGridTextColumn()
            {
                Binding = new Binding("CustomerCode"),
                Header = "Customer",
                Width = 250
            };
            this.grvData.Columns.Add(colName);
        }

        private void InitializeData()
        {

            var itemSource = BaseBL<Customer>.GetAll();
            if (itemSource.IsNullOrEmpty()) return;

            this.grvData.ItemsSource = DeliveryManagementBL.GetDeliveryForLookup();
        }


        #region Events
        private void IntializedEvents()
        {
            this.grvData.MouseDoubleClick += grvData_MouseDoubleClick;
        }

        private void grvData_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.grvData.SelectedItem != null)
            {
                this.OnFormSendSelected(this, EventArgs.Empty);
                this.Close();
            }
        }
        #endregion
    }
}